package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type UserDefine struct {
	UserID uint `gorm_where:"primaryKey"`
	// Name   string `gorm_where:"column:user_name;type:varchar(50);index:ide_user_name"`
	Name string `gorm_where:"column:user_name;type:varchar(50);index:ide_user_name;unique;default:'bobby'"`
}

func main() {
	dsn := "root:root@tcp(192.168.4.111:3306)/mytest?charset=utf8mb4&parseTime=True&loc=Local"
	newLogger := logger.New(
		log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer（日志输出的目标，前缀和日志包含的内容——译者注）
		logger.Config{
			SlowThreshold: time.Second, // 慢 SQL 阈值
			LogLevel:      logger.Info, // 日志级别
			Colorful:      true,        // 禁用彩色打印
		},
	)

	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: newLogger,
	})
	if err != nil {
		fmt.Println("err", err)
	}

	err = db.AutoMigrate(&UserDefine{})
	if err != nil {
		fmt.Println("err", err)
	}

	db.Create(&UserDefine{})
}
